import { reactive, ref, computed } from 'vue';
import * as Store from "@/store";
import Bus from "@/utils/bus";

const useContent = () => {
  const FilmRead = Store.useFilmRead();
  const interfaceData = FilmRead.interfaceData
  const dictionaryData = FilmRead.dictionaryData
  /**
   * 中间图像区域数据新增方法
   * @param createObj 当前图像数据追加的集合
   */
  const imageDataAdd = (createObj: any) => {
    dictionaryData.imageDataList && (dictionaryData.imageDataList[createObj.id] = createObj);
  }

  const imageDataUpdate = (lesionId: number, changeObj: any) => {
    if(dictionaryData.imageDataList){
      const imageData = dictionaryData.imageDataList[lesionId];
      // 改变相应的值
      for(let key in changeObj){
        // imageData[key] && (imageData[key].result = changeObj[key])
        imageData[key] && (imageData[key] = changeObj[key])
      }
    }
  }

  /**
   * 删除中间图像数据方法
   * @param lesionId 病灶ID
   */
  const imageDataDel = (lesionId: number) => {
    dictionaryData.imageDataList && delete dictionaryData.imageDataList[lesionId];
    // if (dictionaryData.imageDataList) {
    //   if (interfaceData.pageSettingList.imageLayout === 'VIDEO_ANNO' && frameId) {
    //     // 通过当前帧段id获取病灶id
    //     // if (Array.isArray(dictionaryData.imageDataList)) {
    //     //   dictionaryData.imageDataList[frameId] = dictionaryData.imageDataList.filter(item => item.id != lesionId);
    //     // }
    //   } else {
    //     // 删除选中病灶和删除未选中病灶逻辑
    //     delete dictionaryData.imageDataList[lesionId];
    //   }
    // }
  }

  /**
   * 根据帧段删除删除中间图层数据
   * @param frameId 帧段ID
   */
  const imageFrameDel = (frameId: number) => {
    let lesionIds: number[] = []
    // 获取当前帧段id下的所有病灶id
    dictionaryData.lesionDataList && Array.isArray(dictionaryData.lesionDataList[frameId]) && (lesionIds = dictionaryData.lesionDataList[frameId].map(item => item.id))
    // 删除存在的id数据
    lesionIds.length !== 0 && lesionIds.map(item => {
      dictionaryData.imageDataList && delete dictionaryData.imageDataList[item]
    })
  }

  /**
   * 影像病灶高亮
   * @param lesionId 病灶id
   * @returns 
   */
  const setLesionActive = (lesionId: number) => {
    if(!dictionaryData.imageDataList) return
    if(!dictionaryData.imageDataList[lesionId]) return
    const item = dictionaryData.imageDataList[lesionId]
    // 抛出事件
    Bus.emit("lesionClick", ref({
      currentItem: { ...item},
      type: 'lesion',
      isLesion: lesionId.toString(),
      id: +lesionId,
    }).value);
  }

  return {
    imageDataAdd,
    imageDataUpdate,
    imageDataDel,
    setLesionActive,
    imageFrameDel
  }
};

export default useContent;